//
//  WBNewFeatureViewController.swift
//  微博
//
//  Created by lipengfei on 15/9/6.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

private let WBNewFeatuerCellID = "Cell"
private let WBNewFeatureCount = 4

class WBNewFeatureViewController: UICollectionViewController {

///  初始化构造函数
     init() {
        //目标构造函数，当类被创建的时候，默认执行的构造函数
        //重写init构造函数，目的是，创建collectionView的时候，为其绑定流水布局
        super.init(collectionViewLayout:UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 程序被加载之后执行
        // 即 所有的内容都被加载了，但是还没有被显示出来
        // 注册重用cell
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        self.collectionView?.registerClass(WBNewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatuerCellID)
        //准备布局
        prepareLayout()
    }
    //collectionView的布局
    private func prepareLayout(){
        let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        //cell之间的间隙
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //禁止弹簧效果
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    
    }
    // MARK: UICollectionViewDataSource 数据源方法

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatuerCellID, forIndexPath: indexPath) as! WBNewFeatureCell
         cell.iconViewIndex = indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == WBNewFeatureCount - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(path) as! WBNewFeatureCell
            cell.showStartButton()
        }
    }

}
private class WBNewFeatureCell: UICollectionViewCell {
    
///  init 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
///  懒加载
    private lazy var iconView = UIImageView()
////  iconView的索引属性
    private var iconViewIndex:Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(iconViewIndex + 1)")
            btn.hidden = true
            
        }
    }
    /// btn 属性，用于 进入主程序
    private lazy var btn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState:   UIControlState.Selected)
        btn.setTitle("进入体验", forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickBtnToJump", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    ///btn 跳转的方法
    @objc private func clickBtnToJump(){
//        发送通知，delegateAPP，让它进行跳转
        NSNotificationCenter.defaultCenter().postNotificationName("WBJumpToController", object: nil)
    }
    /// 为btn 添加动画
    func showStartButton()
    {
        //为btn 添加动画
        btn.hidden = false
        btn.transform = CGAffineTransformMakeScale(0,0)
        print("--------\(self.btn.transform)---------")
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.btn.transform = CGAffineTransformIdentity
            print("--------\(self.btn.transform)---------")
            }) { (_) -> Void in
                
        }
}
///  设置界面
    
    private func setupUI (){
    //向界面中添加iconView
        addSubview(iconView)
       addSubview(btn)
        
        iconView.frame = self.bounds
        
        //btn的约束
        btn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
